//判断字符串是否为空
const stringIsEmpty = function (obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}

const stringIsEmptyShowToast = function(obj,title,duration=2000){
	if(this.stringIsEmpty(obj)){
		uni.showToast({
			title:title,
			duration:duration,
			icon:'none',
		})
		// console.log(title);
		return false;
	} else {
		return true;
	}
}

//点击需要登录的方法
const needLoginJump = function(toUrl,jumpType=1){
	var p = this.authLogin();
	p.then(function(isLogin){
		if(isLogin){
			if(jumpType == 1){
				uni.navigateTo({
					url:toUrl,
				})
			} else if(jumpType == 2){
				// console.log('switchTab===============');
				uni.switchTab({
					url:toUrl,
					success: (e) => {
						var page = getCurrentPages().pop();
						if (page == undefined || page == null) return;
						page.onLoad();
					}
				})
			}
			
		} else {
			uni.showToast({
				title: '为了更好的体验，需要授权登录！',
				mask: true,
				icon: 'none'
			})
		}
	});
}


//用于弹出用户授权
const authLogin = function (){
	return new Promise(function(resolve, reject) {
		// uni.setStorageSync('openid', res.data.data.openid);
		let openid = uni.getStorageSync('openid');
		if(stringIsEmpty(openid)){
			uni.getUserProfile({
			      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			      success: (res) => {
					  // console.log(res.userInfo.nickName);
					  
					  let userObject = res.userInfo;
					  uni.login({
					  	success:(res)=>{
					  		if(res.errMsg == "login:ok"){
					  			
					  			let code = res.code;
					  			uni.request({
					  				header: {
					  					'content-type': 'application/x-www-form-urlencoded'
					  				},
					  				// url: 'https://st.lingyuncmy.com/wxlogin/'+res.code+"/"+encodeURIComponent(userString),
					  				url:"https://sccy.lishiketang.com/index.php/api/user/wxlogin",
					  				method: 'POST',//接口类型 
					  				data: {
					  					'code':code,
					  					'avatar_url':userObject.avatarUrl,
					  					'city':userObject.city,
					  					'province':userObject.province,
					  					'gender':userObject.gender,
					  					'nick_name':userObject.nickName
					  				}, //接口需要的数据
					  				success: function(res) {
					  					console.log(res);
					  					if (res.data.code == 1) {
					  						uni.setStorageSync('openid', res.data.data.openid);
											uni.setStorageSync('gender', userObject.gender);
											uni.setStorageSync('avatarUrl', userObject.avatarUrl);
											uni.setStorageSync('nickName', userObject.nickName);
											// uni.setStorageSync('tishi_count', 3);
											resolve(true);
					  						// that.login(res.data); //将接口返回的数据保存在全局变量中.
					  						//登录成功跳转首页或者你想跳转的地方... 
					  						//注意：如果时导航页 请用uni.switchTab
					  						// 		其他页面建议使用uni.reLaunch
					  					} else {
					  						uni.showToast({
					  							title: '授权登录失败！',
					  							mask: true,
					  							icon: 'none'
					  						})
											resolve(false);
					  					}
					  							
					  							
					  				}
					  							
					  			})
					  			
					  		}
					  		
					  	}
					  })
					  
					  
			      },
				  fail:(res)=>{
					 uni.showToast({
					 	title: '为了更好的体验，需要授权登录！',
					 	mask: true,
					 	icon: 'none'
					 })
					 resolve(false);
				  }
			    })
		} else {
			console.log('客户端判断已经登录');
			resolve(true);
		} 
	
		}).catch((e) => {
			console.log(e)
		});
}


module.exports = {
	stringIsEmpty,
	stringIsEmptyShowToast,
	authLogin,
	needLoginJump,
}